<?php
class TikTokVideoService {
    private static $instance;

    public static function get() {
        if (!self::$instance) {
            self::$instance = new TikTokVideoService();
        }
        return self::$instance;
    }

    protected function __construct() {

    }

    /**
     * 获取数据
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getDataInfo($userId,$reqTime) {
        $res = array(
            'code' => 0,
            'water' => array(),
            'ticket' => array(),
        );

        $waterNum = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokWaterReward', true)['value'];
        $ticketNum = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokCouponReward', true)['value'];
        $waterLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokWaterVideoTime', true)['value'];
        $ticketLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokCouponVideoTime', true)['value'];
        $res['water'] = array(
            'conf' => $waterNum,
            'limit' => $waterLimit,
            'today' => 0
        );
        $res['ticket'] = array(
            'conf' => $ticketNum,
            'limit' => $ticketLimit,
            'today' => 0
        );

        //获取玩家当日已看缓存数据
        $redisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);
        $nowString = $redisDb->get(GameConst::CACHE_KEY_DOUYIN_VIDEO_DATA.$userId);
        if ($nowString != "" || $nowString != null) {
            $nowData = explode(':', $nowString);
            if (!empty($nowData)) {
                $startTime = strtotime(date('Y-m-d', $nowData[1]));
                $endTime = $startTime + 86400;
                if ($reqTime <= $startTime) {
                    $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['REQUEST_TIME_ERR'];//请求时间错误
                    return $res;
                }
                if ($reqTime >= $endTime) {
                    $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_GOLD . $userId, '0:' . $reqTime);
                    $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_TICKET . $userId, '0:' . $reqTime);
                    $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_WATER . $userId, '0:' . $reqTime);
                }
                if ($reqTime > $startTime && $reqTime < $endTime) {
                    $ticketString = $redisDb->get(GameConst::CACHE_KEY_DOUYIN_VIDEO_TICKET.$userId);
                    if ($ticketString){
                        $ticketData = explode(':',$ticketString);
                        if (!empty($ticketData)){
                            $res['ticket']['today'] = $ticketData[0];
                        }
                    }
                    $waterString = $redisDb->get(GameConst::CACHE_KEY_DOUYIN_VIDEO_WATER.$userId);
                    if ($waterString){
                        $waterData = explode(':',$waterString);
                        if (!empty($waterData)){
                            $res['water']['today'] = $waterData[0];
                        }
                    }
                }
            }
        }

        return $res;
    }

    /**
     * 视频换金币
     * @param $userId
     * @param $type
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getGold($userId,$type,$reqTime) {
        $res = array(
            'code' => 0,
            'times' => 0,
        );

        //获得每日上限
        $timesLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokVideoMaxTime', true)['value'];
        //获取玩家当日已看缓存数据
        $redisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);

        $nowString = $redisDb->get(GameConst::CACHE_KEY_DOUYIN_VIDEO_DATA.$userId);
        $ticketString = $redisDb->get(GameConst::CACHE_KEY_DOUYIN_VIDEO_TICKET.$userId);
        $waterString = $redisDb->get(GameConst::CACHE_KEY_DOUYIN_VIDEO_WATER.$userId);

        $startTime = strtotime(date('Y-m-d',$reqTime));
        $endTime = $startTime + 86400;
        if ($nowString == "" || $nowString == null){
            $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_DATA.$userId,'1:'.$reqTime);
        }
        else{
            $nowData = explode(':',$nowString);
            if (!empty($nowData)){
                $startTime = strtotime(date('Y-m-d',$nowData[1]));
                $endTime = $startTime + 86400;
                if ($reqTime > $startTime && $reqTime < $endTime){
                    if ($nowData[0] + 1 > $timesLimit){
                        $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['VIDEO_LIMIT_TIMES_ERR'];//超出观看上限
                        return $res;
                    }
                    if ($type == 2 && $ticketString){
                        $ticketLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokCouponVideoTime', true)['value'];
                        $ticketOld = explode(':',$ticketString);
                        if ($ticketOld[0] + 1 > $ticketLimit){
                            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['VIDEO_LIMIT_TICKET_TIMES_ERR'];//超出每日领取好评券上限
                            return $res;
                        }
                    }
                    if ($type == 3 && $waterString){
                        $waterLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokWaterVideoTime', true)['value'];
                        $waterOld = explode(':',$waterString);
                        if ($waterOld[0] + 1 > $waterLimit){
                            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['VIDEO_LIMIT_WATER_TIMES_ERR'];//超出每日领取水滴上限
                            return $res;
                        }
                    }

                    $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_DATA.$userId,($nowData[0] + 1).':'.$reqTime);
                }
                if ($reqTime <= $startTime){
                    $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['REQUEST_TIME_ERR'];//请求时间错误
                    return $res;
                }
                if ($reqTime >= $endTime){
                    $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_DATA.$userId,'1:'.$reqTime);
                }
            }
        }

        if ($type == 1){//金币
            //获得玩家的等级
            $userLevel = UserService::get()->getUserLevel($userId);
            //计算本次需要的金币数量
            $constNum = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokVideoBonus', true)['value'];
            $goldNum = floor(($constNum + $constNum*($userLevel/10)));

            //获得资源
            GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_TOKEN, $goldNum, true);
            CommonFunctionService::get()->reportResourceUseDetails(80000000,$userId,1,GameConst::ENTITY_ACTION_GET_TOKEN.':'.$goldNum.'|','getGold');//获得资源上报
            $res['times'] = $goldNum;
            $goldString = $redisDb->get(GameConst::CACHE_KEY_DOUYIN_VIDEO_GOLD.$userId);
            if ($goldString == "" || $goldString == null){
                $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_GOLD.$userId,'1:'.$reqTime);
            }
            else{
                $goldData = explode(':',$goldString);
                if (!empty($goldData)){
                    if ($reqTime >= $startTime && $reqTime <= $endTime){
                        $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_DATA.$userId,($goldData[0] + 1).':'.$reqTime);
                    }
                }
            }
        }
        if ($type == 2){//好评券
            $ticketNum = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokCouponReward', true)['value'];
            //获得资源
            GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_TICKET, $ticketNum, true);
            CommonFunctionService::get()->reportResourceUseDetails(80000000,$userId,1,GameConst::ENTITY_ACTION_GET_TICKET.':'.$ticketNum.'|','getTicket');//获得资源上报
            $res['times'] = $ticketNum;

            if (!$ticketString){
                $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_TICKET.$userId,'1:'.$reqTime);
            }
            else{
                $ticketData = explode(':',$ticketString);
                if (!empty($ticketData)){
                    if ($reqTime >= $startTime && $reqTime <= $endTime){
                        $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_TICKET.$userId,($ticketData[0] + 1).':'.$reqTime);
                    }
                }
            }
        }
        if ($type == 3){//水
            $waterNum = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokWaterReward', true)['value'];
            //获得资源
            GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_WATER, $waterNum, true);
            CommonFunctionService::get()->reportResourceUseDetails(80000000,$userId,1,GameConst::ENTITY_ACTION_GET_WATER.':'.$waterNum.'|','getWater');//获得资源上报
            $res['times'] = $waterNum;

            if (!$waterString){
                $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_WATER.$userId,'1:'.$reqTime);
            }
            else{
                $waterData = explode(':',$waterString);
                if (!empty($waterData)){
                    if ($reqTime >= $startTime && $reqTime <= $endTime){
                        $redisDb->set(GameConst::CACHE_KEY_DOUYIN_VIDEO_WATER.$userId,($waterData[0] + 1).':'.$reqTime);
                    }
                }
            }
        }

        return $res;
    }

    /**
     * 升级视频换金币与道具
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getLevelUp($userId,$reqTime) {
        $res = array(
            'code' => 0
        );

        //获得玩家的等级
        $userLevel = UserService::get()->getUserLevel($userId);

        //发送剩余的等级道具 2倍
        $profile = SzPersister::get()->getVo($userId, ModuleProfileConst::ORM_NAME_PROFILE);
        $items = array(); // for bonus items when profile levelup
        $canUpgradeLastLevel = true;
        $continuousMode = false;
        $platform = 'tt';
        $entityData= UserService::get()->processTTUserExpUpdate($profile, $items, $canUpgradeLastLevel, $continuousMode,$platform);

        if ($entityData && is_array($entityData)) {
            $tid = '';
            foreach ($entityData as $entityId => $count) {
                error_log('TIKO_entityId==>'.$entityId."\r\n",3,$_ENV['LOG_ROOT'] . '/payment-' . date('Y-m-d') . '.log');
                error_log('TIKO_count==>'.$count."\r\n",3,$_ENV['LOG_ROOT'] . '/payment-' . date('Y-m-d') . '.log');
                GameService::get()->handleEntity($userId, $entityId, $count, true, true);
                $tid .= $entityId . ':' . abs($count) . '|';
            }
            CommonFunctionService::get()->reportResourceUseDetails(99999988,$userId,1,$tid,'updateExp');//获得资源上报

        }

        return $res;
    }



}
